package com.youxin.busfoundation.common.es;

import com.youxin.busfoundation.common.es.bean.CompanyBaseBean;
import com.youxin.busfoundation.common.es.bean.CompanyIndexBean;
import com.youxin.busfoundation.common.es.bean.ContactBean;
import com.youxin.foundation.utils.JsonUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @desc ES企业操作
 * @author Kris.wang
 * @date 2017-12-19
 */
public class CompanyHandle extends BaseHandle{

    // Index索引类型
    private static final String TYPE  = "sls_company";

    /**
     * @desc 创建企业索引文档
     * @param id
     * @param type 类型：business-业务 spider-爬虫
     * @param bean
     * @return
     * @throws Exception
     */
    public boolean createCompanyIndex( String id, String type, CompanyIndexBean bean) throws Exception
    {
        // 文档ID
        if (id == null || id.isEmpty()) {
            return false;
        }
        // 类型必须是business|spider
        String types[] = {"business","spider"};
        List<String> typeList= Arrays.asList(types);
        if (!typeList.contains(type)) {
           return false;
        }
        // 文档必须存在一条记录
        Map<String, Object> source = objectToMap(bean);
        if (source.size() == 0) {
            return false;
        }
        return createIndex(TYPE, type, id, source);
    }

    /**
     * @desc 获取企业索引文档
     * @param id
     * @return
     * @throws Exception
     */
    public CompanyBaseBean getCompanyIndex(String id) throws Exception
    {
        CompanyBaseBean bean = JsonUtils.stringToFasterxmlObject(getIndex(TYPE, "business", id).toString(), CompanyBaseBean.class);
        if (bean==null) {
             bean = JsonUtils.stringToFasterxmlObject(getIndex(TYPE, "spider", id).toString(), CompanyBaseBean.class);
        }
        return bean;
    }

    /**
     * @desc 更新企业索引文档
     * @param id
     * @param type 类型：business-业务 spider-爬虫
     * @param bean
     * @return
     * @throws Exception
     */
    public boolean updateCompanyIndex( String id, String type, CompanyIndexBean bean) throws Exception
    {
        // 文档ID
        if (id == null || id.isEmpty()) {
            return false;
        }
        // 类型必须是business|spider
        String types[] = {"business","spider"};
        List<String> typeList= Arrays.asList(types);
        if (!typeList.contains(type)) {
            return false;
        }
        // 文档必须存在一条记录
        Map<String, Object> doc = objectToMap(bean);
        if (doc.size() == 0) {
            return false;
        }
        return updateIndex(TYPE, type, id, doc);
    }

    /**
     * @desc 删除企业索引文档
     * @param id
     * @param type 类型：business-业务 spider-爬虫
     * @return
     * @throws Exception
     */
    public boolean deleteCompanyIndex( String id, String type) throws Exception
    {
        // 文档ID
        if (id == null || id.isEmpty()) {
            return false;
        }
        // 类型必须是business|spider
        String types[] = {"business","spider"};
        List<String> typeList= Arrays.asList(types);
        if (!typeList.contains(type)) {
            return false;
        }
        return deleteIndex(TYPE, type, id);
    }

}
